//
//  LastPicCollectionViewCell.m
//  miaopai
//
//  Created by 杜昕宇 on 15/11/16.
//  Copyright © 2015年 Crow Zhang. All rights reserved.
//

#import "LastPicCollectionViewCell.h"
#import "MacroDefinition.h"
#import <UIImageView+WebCache.h>
#import "CoreData.h"
#import "CollectionPicture.h"
@interface LastPicCollectionViewCell()
@property (nonatomic, retain) CoreData *coreDataManager;

@property (nonatomic, retain) UILabel *timeLabel;// 时间

@property (nonatomic, retain) UIView *lineView; // 时间下面的线

@property (nonatomic, retain) UILabel *detaiLabel; // 时间下面的 label;

@property (nonatomic, retain) UIButton *backView; // 视屏以及 gif 的 view

@property (nonatomic, retain) UIButton *likedButton; // 收藏

@property (nonatomic, retain) UIButton *shareButton; // 分享

@property (nonatomic, retain) UIImageView *backImage; // 图片

@property (nonatomic, copy) shareBlcok myBlock;
@end

@implementation LastPicCollectionViewCell

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        self.contentView.backgroundColor = [UIColor whiteColor];
        self.coreDataManager = [CoreData shareManagerWithFile:@"joke"];
        [self creatView];
    }
    return self;
}

- (void)dealloc
{
    Block_release(_myBlock);
    [_coreDataManager release];
    [_somePic release];
    [_backImage release];
    [_lastPic release];
    [_lineView release];
    [_likedButton release];
    [_timeLabel release];
    [_detaiLabel release];
    [_shareButton release];
    [super dealloc];
}

#pragma mark set方法
- (void)setLastPic:(LastPicture *)lastPic{
    if (lastPic != _lastPic) {
        [_lastPic release];
        _lastPic = [lastPic retain];
        _timeLabel.text = lastPic.created_at;
        _detaiLabel.text = lastPic.text;
    }
    CGFloat vWidth = SCREEN_WIDTH - 30;
    CGFloat vHeight = SCREEN_HEIGHT / 20;
    CGFloat imageHeight = SCREEN_HEIGHT / 30;
    _timeLabel.frame = CGRectMake(10, 0, vWidth , vHeight);
    _lineView.frame = CGRectMake(0, _timeLabel.Height + _timeLabel.Y, vWidth + 10, 1);
#warning 自适应高度
    _detaiLabel.frame = CGRectMake(_timeLabel.X, _timeLabel.Y + _timeLabel.Height + 5, vWidth, [[self class] heightOfText:_lastPic.text]);

    BOOL Screen_width = [_lastPic.width floatValue] < (SCREEN_WIDTH - 20) ;
    if (Screen_width) {
        _backView.frame = CGRectMake(0, _detaiLabel.Y + _detaiLabel.Height + 5, [_lastPic.width floatValue], [_lastPic.height floatValue]);
        [_backView setCenter:CGPointMake(vWidth / 2, _backView.Y + [_lastPic.height floatValue] / 2)];
    } else {
        CGFloat width = [_lastPic.width floatValue] / SCREEN_WIDTH;
        CGFloat height = [_lastPic.height floatValue] / width;
        _backView.frame = CGRectMake(0, _detaiLabel.Y + _detaiLabel.Height , SCREEN_WIDTH - 20, height);
    }
    _likedButton.frame = CGRectMake(_detaiLabel.X, _backView.Y + _backView.Height + 5, imageHeight, imageHeight);
    _backImage.frame = CGRectMake(0, 0, _backView.Width, _backView.Height);
    [_backImage sd_setImageWithURL:[NSURL URLWithString:_lastPic.original]];
    _shareButton.frame = CGRectMake( vWidth - 10 - imageHeight, _backView.Y + _backView.Height + 5,  imageHeight, imageHeight);

    NSString *str = [self returnResult] == YES ? @"liked" : @"like";
    [_likedButton setImage:[UIImage imageNamed:str] forState:UIControlStateNormal];
}


- (void)setSomePic:(SomePicture *)somePic{
    if (somePic != _somePic) {
        [_somePic release];
        _somePic = [somePic retain];
        _timeLabel.text = somePic.created_at;
        _detaiLabel.text = somePic.text;
    }
    CGFloat vWidth = SCREEN_WIDTH - 30;
    CGFloat vHeight = SCREEN_HEIGHT / 20;
    CGFloat imageHeight = SCREEN_HEIGHT / 30;
    _timeLabel.frame = CGRectMake(10, 0, vWidth , vHeight);
    _lineView.frame = CGRectMake(0, _timeLabel.Height + _timeLabel.Y, vWidth + 10, 1);
#warning 自适应高度
    _detaiLabel.frame = CGRectMake(_timeLabel.X, _timeLabel.Y + _timeLabel.Height + 5, vWidth, [[self class] heightOfText:_somePic.text]);

    BOOL Screen_width = [_somePic.width floatValue] < (SCREEN_WIDTH - 20) ;
    if (Screen_width) {
        _backView.frame = CGRectMake(0, _detaiLabel.Y + _detaiLabel.Height + 5, [_somePic.width floatValue], [_somePic.height floatValue]);
        [_backView setCenter:CGPointMake(vWidth / 2, _backView.Y + [_somePic.height floatValue] / 2)];
    } else {
        CGFloat width = [_somePic.width floatValue] / SCREEN_WIDTH;
        CGFloat height = [_somePic.height floatValue] / width;
        _backView.frame = CGRectMake(0, _detaiLabel.Y + _detaiLabel.Height , SCREEN_WIDTH - 20, height);
    }
    _likedButton.frame = CGRectMake(_detaiLabel.X, _backView.Y + _backView.Height + 5, imageHeight, imageHeight);
    _backImage.frame = CGRectMake(0, 0, _backView.Width, _backView.Height);
    [_backImage sd_setImageWithURL:[NSURL URLWithString:_somePic.original]];
    _shareButton.frame = CGRectMake( vWidth - 10 - imageHeight, _backView.Y + _backView.Height + 5,  imageHeight, imageHeight);
    NSString *str = [self returnResult] == YES ? @"liked" : @"like";
    [_likedButton setImage:[UIImage imageNamed:str] forState:UIControlStateNormal];
}


- (void)creatView{

/******************* 时间 label ********************/
    self.timeLabel = [[UILabel alloc] init];
//    _timeLabel.backgroundColor = [UIColor cyanColor];
    [self.contentView addSubview:_timeLabel];
    [_timeLabel release];

/********************线的 view ***********************/
    self.lineView = [[UIView alloc] init];
    _lineView.backgroundColor = [UIColor lightGrayColor];
    [self.contentView addSubview:_lineView];
    [_lineView release];

/********************* 详情的 label*******************/
    self.detaiLabel = [[UILabel alloc] init];
//    _detaiLabel.backgroundColor = [UIColor cyanColor];
    _detaiLabel.numberOfLines = 0;
    [self.contentView addSubview:_detaiLabel];
    [_detaiLabel release];

/******************** backView ************************/
    self.backView = [[UIButton alloc] init];
    _backView.backgroundColor = [UIColor cyanColor];
    [self.contentView addSubview:_backView];
    [_backView release];


    self.backImage = [[UIImageView alloc] init];
    _backImage.backgroundColor = [UIColor whiteColor];
    _backImage.userInteractionEnabled = YES;
    [_backView addSubview:_backImage];
    [_backImage release];
/**********************喜欢****************************/
    self.likedButton = [[UIButton alloc] init];
//    _likedButton.backgroundColor = [UIColor cyanColor];
    [_likedButton addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
    [_likedButton setImage:[UIImage imageNamed:@"like"] forState:UIControlStateNormal];
    [self.contentView addSubview:_likedButton];
    [_likedButton release];

/************************共享***************************/
    self.shareButton = [[UIButton alloc] init];
//    _shareButton.backgroundColor = [UIColor cyanColor];
    [_shareButton addTarget:self action:@selector(shareAction:) forControlEvents:UIControlEventTouchUpInside];
    [_shareButton setImage:[UIImage imageNamed:@"share"] forState:UIControlStateNormal];
    [self.contentView addSubview:_shareButton];
    [_shareButton release];
}

- (BOOL)returnResult{
    NSArray *arr = [self returnArray];
    if (arr.count != 0) {
        return YES;
    }
    return NO;
}

- (NSArray *)returnArray{

    NSFetchRequest *request = [[NSFetchRequest alloc] initWithEntityName:@"CollectionPicture"];
    NSString *str;
    if (_somePic) {
        str = _somePic.text;
    } else if (_lastPic) {
        str = _lastPic.text;
    }
    request.predicate = [NSPredicate predicateWithFormat:[NSString stringWithFormat:@"text = '%@'", str]];
    NSArray *arr = [_coreDataManager.managedObjectContext executeFetchRequest:request error:nil];
    NSLog(@"%@", arr);
    return arr;
}
//    收藏
- (void)buttonAction:(UIButton *)button{
    NSLog(@"我被收藏了");
    NSArray *arr = [self returnArray];
    NSLog(@"%@", arr);
    if (arr.count == 0) {
        CollectionPicture *pic = [NSEntityDescription insertNewObjectForEntityForName:@"CollectionPicture" inManagedObjectContext:_coreDataManager.managedObjectContext];
        if (_lastPic) {
            pic.height = _lastPic.height;
            pic.width = _lastPic.width;
            pic.created_at = _lastPic.created_at;
            pic.original = _lastPic.original;
            pic.playurl = _lastPic.playurl;
            pic.text = _lastPic.text;
        } else if (_somePic) {
            pic.height = _somePic.height;
            pic.width = _somePic.width;
            pic.created_at = _somePic.created_at;
            pic.original = _somePic.original;
            pic.playurl = _somePic.playurl;
            pic.text = _somePic.text;
        }
        [_coreDataManager saveContext];
        [_likedButton setImage:[UIImage imageNamed:@"liked"] forState:UIControlStateNormal];
    } else if (arr.count != 0) {
        for (CollectionPicture *pic in arr) {
            [_coreDataManager.managedObjectContext deleteObject:pic];
            [_coreDataManager saveContext];
        }
        [_likedButton setImage:[UIImage imageNamed:@"like"] forState:UIControlStateNormal];
    }

}

- (void)shareButtonClick:(shareBlcok)block
{
    self.myBlock = block;
}
- (void)shareAction:(UIButton *)button{
    self.myBlock();
}

+ (CGFloat)heightOfText:(NSString *)text{

    CGSize size = CGSizeMake(SCREEN_WIDTH - 30, MAXFLOAT);
    NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:[UIFont systemFontOfSize:17], NSFontAttributeName, nil];
    CGRect label = [text boundingRectWithSize:size options:NSStringDrawingUsesLineFragmentOrigin attributes:dic context:nil];
    return label.size.height;
}

@end
